package com.dunqi.gpm.shunqing.home.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.dunqi.gpm.shunqing.R;
import com.dunqi.gpm.shunqing.common.api.ApiFactory;
import com.dunqi.gpm.shunqing.common.api.RxSchedulers;
import com.dunqi.gpm.shunqing.common.base.BaseFragment;
import com.dunqi.gpm.shunqing.common.util.SwipeUtil;
import com.dunqi.gpm.shunqing.home.activity.ProjectProgressDetailsActivity;
import com.dunqi.gpm.shunqing.home.activity.UpdateProgressActivity;
import com.dunqi.gpm.shunqing.home.adapter.ProjectTimelineAdapter;
import com.dunqi.gpm.shunqing.home.bean.Page;
import com.dunqi.gpm.shunqing.home.bean.ProjectTimeline;
import com.dunqi.gpm.shunqing.home.widget.EmptyView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.reactivex.functions.Consumer;

/**
 * Created by 空 on 2017/11/22 0022.
 */

public class ProjectProgressFragment extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener, BaseQuickAdapter.RequestLoadMoreListener, BaseQuickAdapter.OnItemClickListener {

    @BindView(R.id.recycler)
    RecyclerView recycler;
    @BindView(R.id.swipeRefresh)
    SwipeRefreshLayout swipeRefresh;

    private ProjectTimelineAdapter adapter;
    List<ProjectTimeline> data = new ArrayList<>();

    @Override
    protected void refreshData(Bundle savedInstanceState) {
        net();
    }

    private void net() {
        ApiFactory.getInstance()
                .queryOwnedProgressList("-1,0,3",page,true)
                .compose(RxSchedulers.<Page<ProjectTimeline>>io_main())
                .subscribe(new Consumer<Page<ProjectTimeline>>() {
                    @Override
                    public void accept(Page<ProjectTimeline> projectTimelinePage) throws Exception {
                        SwipeUtil.loadCompleted(swipeRefresh);
                        ArrayList<ProjectTimeline> list = projectTimelinePage.getList();
                        if (page == 1) {
                            data.clear();
                        }
                        data.addAll(list);
                        if (projectTimelinePage.isHasNextPage()) {
                            adapter.loadMoreComplete();
                        } else {
                            adapter.loadMoreEnd();
                        }
                        adapter.notifyDataSetChanged();
                        page++;
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        adapter.loadMoreFail();
                        SwipeUtil.loadCompleted(swipeRefresh);
                    }
                });
    }

    @Override
    protected void initUiAndListener() {
        SwipeUtil.init(swipeRefresh);
        recycler.setLayoutManager(new LinearLayoutManager(context));
        adapter = new ProjectTimelineAdapter(context, data);
        recycler.setAdapter(adapter);
        adapter.setEmptyView(new EmptyView(context));
        swipeRefresh.setOnRefreshListener(this);
        adapter.setOnLoadMoreListener(this, recycler);
        adapter.setOnItemClickListener(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_time_line;
    }

    public static ProjectProgressFragment newInstance() {
        ProjectProgressFragment fragment = new ProjectProgressFragment();
        return fragment;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == UpdateProgressActivity.RESULT_CODE) {
            swipeRefresh.setRefreshing(true);
            onRefresh();
        }

    }

    @Override
    public void onRefresh() {
        page = 1;
        net();
    }

    @Override
    public void onLoadMoreRequested() {
        net();
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        ProjectProgressDetailsActivity.start((Activity) context, data.get(position).getProPlanProgressId());
    }
}